<?php

namespace app\organization\controller\trainer;

use app\common\controller\Frontend;
use app\common\enums\core\AuditStateEnum;
use app\common\enums\TrainerAuditTypeEnum;
use app\common\model\banxi\trainer\TrainerAudit as TrainerAuditModel;
use think\db\exception\DbException;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

/**
 * 课程字典
 */
class TrainerAudit extends Frontend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new TrainerAuditModel();
    }

    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        $param['query']['EQ_organizationId'] = $this->auth->organizationId;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);

        $list = $this->model->getListPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->getInfo($where);
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $this->ok('操作成功', $info);
    }

    public function del()
    {
        $param = $this->request->param(['ids']);
        try {
            $validate = Validate::rule([
                'ids' => ValidateRule::isRequire(null, '请选择你要删除的教练审核'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $this->model->del($param['ids']);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok();
    }

    public function updateInfo(): void
    {
        $param = $this->request->param();
        try {
            $id = $param['id'];
            $trainerAudit = $this->model->where(['id' => $id])->find();
            if (empty($trainerAudit)) {
                throw new ValidateException('操作失败，数据不存在');
            }
            $organizationId = $this->auth->organizationId;
            if ($trainerAudit['organizationId'] != $organizationId) {
                throw new ValidateException('操作失败，数据不存在');
            }
            $applyState = $trainerAudit['applyState'];
            $auditType = $trainerAudit['auditType'];
            if ($auditType != TrainerAuditTypeEnum::ADD) {
                throw new ValidateException('操作失败，数据不允许被更改');
            }
            if ($applyState != AuditStateEnum::E3) {
                throw new ValidateException('操作失败，数据不允许被更改');
            }
            $trainerAudit->save(['ApplyState' => AuditStateEnum::E1]);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok();
    }
}